import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Service implements ActionListener {

    private final JFrame frame = new JFrame("创建五子棋游戏");
    private final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 55, 10));
    private final JPanel optionPanel = new JPanel(new BorderLayout(10, 10));
    private final JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
    private final JButton startBtn = new JButton("开启联机");
    private final JButton stopBtn = new JButton("结束联机");
    private final JTextArea textArea = new JTextArea(10,30);

    private Thread serverThread;

    private void initUI() {
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //关闭窗口会结束
        frame.setLayout(new BorderLayout());      // 设置
        frame.setResizable(false);  // 禁止窗口调整大小
        frame.setAlwaysOnTop(true);  // 窗口始终置顶

        Dimension btnSize = new Dimension(110, 35); // 宽度150px，高度50px
        buttonPanel.setVisible(true);

        optionPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));


        optionPanel.setVisible(true);

        // 添加到窗口底部
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.add(optionPanel, BorderLayout.CENTER);

        startBtn.setBackground(Color.GRAY);
        startBtn.setFont(new Font("宋体", Font.PLAIN, 15));
        startBtn.setPreferredSize(btnSize);
        startBtn.setActionCommand("startBtn");
        startBtn.addActionListener(this);
        startBtn.setVisible(true);
        startBtn.setEnabled(true);
        buttonPanel.add(startBtn);

        stopBtn.setBackground(Color.GRAY);
        stopBtn.setFont(new Font("宋体", Font.PLAIN, 15));
        stopBtn.setPreferredSize(btnSize);  //按钮大小
        stopBtn.setActionCommand("stopBtn");
        stopBtn.addActionListener(this);
        stopBtn.setVisible(true);
        stopBtn.setEnabled(false);
        buttonPanel.add(stopBtn);


        optionPanel.add(inputPanel, BorderLayout.NORTH);

        textArea.setFont(new Font("宋体", Font.PLAIN, 14));
        JScrollPane scrollPane = new JScrollPane(textArea);
        optionPanel.add(scrollPane, BorderLayout.CENTER);

        frame.setVisible(true);


    }

    public Service() {
        initUI();
    }

    private void clickStartBtn() {
        Server server = new Server(textArea,frame);
        serverThread = new Thread(server);

        serverThread.start();

        startBtn.setEnabled(false);
        stopBtn.setEnabled(true);
    }

    private void clickStopBtn() {
        serverThread.interrupt();   //中断

        startBtn.setEnabled(true);
        stopBtn.setEnabled(false);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case "startBtn":
                clickStartBtn();
                break;
            case "stopBtn":
                clickStopBtn();
                break;
        }
    }

    public static void main(String[] args) {
        new Service();
    }

}
